#include "data.h"
#include "util.h"
#include <QJsonArray>

namespace model {

int KindAndTag::id = 10000;

KindAndTag::KindAndTag()
{
    QList<QString> kinds = {"历史", "美食", "游戏", "科技", "运动", "动物", "旅行", "电影"};
    QHash<QString, QList<QString>> tags = {
                                           {"历史", {"中国史", "世界史", "历史人物", "艺术", "文化", "奇闻"}},
                                           {"美食", {"美食测评", "美食制作", "美食攻略", "美食记录", "探店", "水果", "海鲜"}},
                                           {"游戏", {"游戏攻略","单机游戏","电子竞技","手机游戏","网络游戏","游戏赛事","棋牌游戏",}},
                                           {"科技", {"数码","软件应用","智能家居","手机","电脑","人工智能","基础设施"}},
                                           {"运动", {"篮球","足球","乒乓球","羽毛球","健身","竞技体育","运动装备"}},
                                           {"动物", {"喵星人","汪星人","宠物知识","动物咨询","野生动物","动物世界","萌宠"}},
                                           {"旅行", {"旅游攻略","旅行vlog","自驾游","交通","环球旅行","露营","野外生存"}},
                                           {"电影", {"电影解说","电影推荐","电影剪辑","搞笑","吐槽","悬疑","经典"}},
                                           };

    // 创建分类及其ID
    for(auto& kind : kinds) {
        kindIds.insert(kind, id++);
    }

    // 创建标签及其ID
    for(auto& kind : kinds) {
        // QHash<QString, QHash<QString,int>> tagIds;
        QHash<QString,int> Tag2Id;
        for(auto& tag : tags[kind]) {
            Tag2Id.insert(tag, id++);
        }

        tagIds.insert(kind, Tag2Id);
    }
}

const QList<QString> KindAndTag::getAllKind() const
{
    return  kindIds.keys();
}

const QHash<QString, int> KindAndTag::getTagsByKind(const QString &kind) const
{
    return tagIds[kind];
}

int KindAndTag::getKindId(const QString &kind) const
{
    return kindIds[kind];
}

int KindAndTag::getTagId(const QString &kind, const QString &tag) const
{
    return tagIds[kind][tag];
}

void VideoInfo::loadVideoInfo(QJsonObject videoInfoObj)
{
    videoId = videoInfoObj.value("videoId").toString();
    userId = videoInfoObj.value("userId").toString();
    userAvatarId = videoInfoObj.value("userAvatarId").toString();
    nickName = videoInfoObj.value("nickname").toString();
    videoFileId = videoInfoObj.value("videoFileId").toString();
    photoFileId = videoInfoObj.value("photoFileId").toString();
    likeCount = videoInfoObj.value("likeCount").toInt();
    playCount = videoInfoObj.value("playCount").toInt();
    videoSize = videoInfoObj.value("videoSize").toInt();
    videoDesc = videoInfoObj.value("videoDesc").toString();
    videoTitle = videoInfoObj.value("videoTitle").toString();
    videoDuration = videoInfoObj.value("videoDuration").toInt();
    videoUpTime = videoInfoObj.value("videoUpTime").toString();
    // 视频状态
    videoStatus = videoInfoObj["videoStatus"].toInt();
    // 审核者id
    checkerId = videoInfoObj["checkerId"].toString();
    // 审核者昵称
    checkerName = videoInfoObj["checkerName"].toString();
    // 审核者用户头像Id
    checkerAvatar = videoInfoObj["checkerAvatar"].toString();
}

void VideoList::setPageIndex(int pageIndex)
{
    this->pageIndex = pageIndex;
}

int VideoList::getPageIndex() const
{
    return this->pageIndex;
}

int VideoList::getVideoListCount() const
{
    return this->videoInfos.size();
}

void VideoList::setVideoTotalCount(int videoTotalCount)
{
    this->videoTotalCount = videoTotalCount;
}

int VideoList::getVideoTotalCount() const
{
    return this->videoTotalCount;
}

void VideoList::addVideo(const VideoInfo &videoInfo)
{
    this->videoInfos.append(videoInfo);
}

const QList<VideoInfo> &VideoList::getVideoList() const
{
    return this->videoInfos;
}

void VideoList::clearVideoList()
{
    this->videoInfos.clear();
    this->videoTotalCount = 0;
    this->pageIndex = 1;
}

void VideoList::incrementPlayNum(const QString &videoId)
{
    for(auto& videoInfo : videoInfos) {
        if(videoInfo.videoId == videoId) {
            videoInfo.playCount++;
            return ;
        }
    }
}

void VideoList::updateLikeNumber(const QString &videoId, int64_t likeCount)
{
    for(auto& videoInfo : videoInfos){
        if(videoInfo.videoId == videoId){
            videoInfo.likeCount = likeCount;
            return;
        }
    }
}

void VideoList::updateVideoCheckInfo(const QString &videoId, VideoStatus videoStatus,
                                    const QString &nickname, const QString &checkerId, const QString &checkerAvatarId)
{
    for(auto& videoInfo : videoInfos){
        if(videoInfo.videoId == videoId){
            videoInfo.videoStatus = videoStatus;
            videoInfo.checkerId = checkerId;
            videoInfo.checkerName = nickname;
            videoInfo.checkerAvatar = checkerAvatarId;
            return;
        }
    }
}

void BarrageInfo::loadBarrageInfo(QJsonObject &barrageObj)
{
    barrageId = barrageObj["barrageId"].toString();
    userId = barrageObj["userId"].toString();
    playTime = barrageObj["barrageTime"].toInt();
    text = barrageObj["barrageContent"].toString();
}

void UserInfo::loadUserInfo(const QJsonObject &userInfoJson)
{
    userId = userInfoJson["userId"].toString();        // 用户Id
    photoNum = userInfoJson["phoneNum"].toString();      // 用户手机号
    nickname = userInfoJson["nickname"].toString();      // 用户昵称
    // 角色类型：普通用户 普通管理员 超级管理员 临时用户
    QJsonArray roleTypeArray = userInfoJson["roleType"].toArray();
    for(int i = 0; i < roleTypeArray.size(); ++i){
        roleType.append(roleTypeArray[i].toInt());
    }

    // 身份类型：B端用户 C端用户
    QJsonArray identityTypeArray = userInfoJson["identityType"].toArray();
    for(int i = 0; i < identityTypeArray.size(); ++i){
        identityType.append(identityTypeArray[i].toInt());
    }

    likeCount = userInfoJson["likeCount"].toInteger();        // 点赞数
    playCount = userInfoJson["playCount"].toInteger();        // 播放数
    followedCount = userInfoJson["followedCount"].toInteger();    // 关注数：当前用户关注其他用户的数量
    followerCount = userInfoJson["followerCount"].toInteger();    // 粉丝数：其他用户管理当前用户的数量
    userStatus = userInfoJson["userStatus"].toInt();           // 用户状态：管理员页面需要用到--启用和禁止
    isFollowing = userInfoJson["isFollowing"].toInt();           // 是否被关注: 主要在其他用户信息中使用
    userMemo = userInfoJson["userMemo"].toString();          // 用户备注信息：新增管理员时需要用到
    userCTime = userInfoJson["userCTime"].toString();         // 用户创建的时间
    avatarFileId = userInfoJson["avatarFileId"].toString();      // 用户头像id
}

bool UserInfo::isBUser() const
{
    for(auto idType : identityType){
        if(idType == BUser){
            return true;
        }
    }

    return false;
}

bool UserInfo::isTempUser() const
{
    for(auto role : roleType){
        if(role == TempUser){
            return true;
        }
    }
    return false;
}

void UserInfo::buildTempUserInfo()
{
    userId = "";               // 用户Id
    photoNum = "";             // 用户手机号
    nickname = "临时用户";      // 用户昵称
    roleType.clear();
    roleType.append(TempUser); // 角色类型：普通用户 普通管理员 超级管理员 临时用户
    //roleType.append(User);
    identityType.clear();
    identityType.append(CUser);// 身份类型：B端用户 C端用户
    likeCount = 0;             // 点赞数
    playCount = 0;             // 播放数
    followedCount = 0;         // 关注数：当前用户关注其他用户的数量
    followerCount = 0;         // 粉丝数：其他用户管理当前用户的数量
    userStatus = 0;            // 用户状态：管理员页面需要用到--启用和禁止
    isFollowing = 0;           // 是否被关注: 主要在其他用户信息中使用
    userMemo = "";             // 用户备注信息：新增管理员时需要用到
    userCTime = "";            // 用户创建的时间
    avatarFileId = "";         // 用户头像id
}

bool UserInfo::isAdminDisable() const
{
    return AdminStatus::disable == userStatus;
}


void AdminInfo::loadAdminInfo(const QJsonObject &adminJson)
{
    userId = adminJson["userId"].toString();
    nickname = adminJson["nickname"].toString();
    roleType = static_cast<RoleType>(adminJson["roleType"].toInt());
    phoneNumber = adminJson["phoneNumber"].toString();
    userStatu = static_cast<AdminStatus>(adminJson["userStatu"].toInt());
    userMemo = adminJson["userMemo"].toString();
}


void AdminList::addAdminInfo(const AdminInfo &adminInfo)
{
    adminInfos.append(adminInfo);
}

void AdminList::setAdminStatus(const QString &userId, AdminStatus adminStatus)
{
    for(auto& adminInfo : adminInfos){
        if(adminInfo.userId == userId){
            adminInfo.userStatu = adminStatus;
            return;
        }
    }
}


} // end of model
